package org.dallasrowing.logbook.ui.view;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class SWTNewRowerWindow implements NewRowerWindow {

	private Shell newRowerEntryDialog;
	private Text name;
	private Combo level;
	private Text email;
	private Text phone;
	private Button isMember;
	private Button createButton;
	private Button cancelButton;
	private Color normalColor;
	private Color redColor;

	public NewRowerWindow createNewRowerWindow(Shell parent) {
		newRowerEntryDialog = new Shell (parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.RESIZE );
		newRowerEntryDialog.setText ("Create new Rower...");
		newRowerEntryDialog.setLayout(new GridLayout(2, true));
		
		createNewEntryElements( newRowerEntryDialog );

		newRowerEntryDialog.setDefaultButton(cancelButton);
		validateEnableSubmit();
		
		newRowerEntryDialog.setMinimumSize(300, 100);
		newRowerEntryDialog.pack();
		newRowerEntryDialog.open(); 
		
		return this;
	}

	private void createNewEntryElements(final Shell parent) {
		Listener enabledListener = new Listener(){
			public void handleEvent(Event arg0) {
				validateEnableSubmit();
			}
		};

		GridData gridData = new GridData( GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING  );
		Label l = new Label(parent, SWT.NONE);
		l.setText("Name:");
		l.setLayoutData( gridData );
		
		gridData = new GridData( GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING );
		name = new Text(parent, SWT.BORDER);
		name.setLayoutData(gridData);
		name.addListener(SWT.Modify, enabledListener);
		
		gridData = new GridData( GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING );
		l = new Label(parent, SWT.NONE);
		l.setText("Level:");
		l.setLayoutData( gridData );
		
		gridData = new GridData( GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING );
		level = new Combo(parent, SWT.DROP_DOWN | SWT.READ_ONLY);
		level.setLayoutData(gridData);
		level.add("3");
		level.add("4");
		level.add("5");
		level.addListener(SWT.Modify, enabledListener);
		
		gridData = new GridData( GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING );
		l = new Label(parent, SWT.NONE);
		l.setText("Email:");
		l.setLayoutData( gridData );
		
		gridData = new GridData( GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING );
		email = new Text(parent, SWT.BORDER);
		email.setLayoutData(gridData);
		email.addListener(SWT.Modify, enabledListener);
		
		gridData = new GridData( GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING );
		l = new Label(parent, SWT.NONE);
		l.setText("Phone:");
		l.setLayoutData( gridData );
		
		gridData = new GridData( GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING );
		phone = new Text(parent, SWT.BORDER);
		phone.setLayoutData(gridData);
		phone.addListener(SWT.Modify, enabledListener);
		
		gridData = new GridData( GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING );
		l = new Label(parent, SWT.NONE);
		l.setText("Member of DRC:");
		l.setLayoutData( gridData );
		
		gridData = new GridData( GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING );
		isMember = new Button(parent, SWT.CHECK);
		isMember.setLayoutData(gridData);
		
		gridData = new GridData( GridData.HORIZONTAL_ALIGN_CENTER );
		createButton = new Button( parent, SWT.PUSH);
		createButton.setText("Submit");
		createButton.setEnabled(false);
		createButton.setLayoutData(gridData);

		gridData = new GridData( GridData.HORIZONTAL_ALIGN_CENTER );
		cancelButton = new Button( parent, SWT.PUSH);
		cancelButton.setText("Cancel");
		cancelButton.setLayoutData(gridData);
		cancelButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				parent.dispose();
			}
		});

		normalColor = name.getForeground();
		redColor = parent.getDisplay().getSystemColor(SWT.COLOR_RED);
		
	}

	private void validateEnableSubmit() {
		boolean enable = true;
		
		if (isNameValid())
		{
			name.setForeground(normalColor);
		}
		else
		{
			enable = false;
			name.setForeground(redColor);
		}
		
		if (!isLevelValid())
		{
			enable = false;
		}
		
		if (isEmailValid())
		{
			email.setForeground(normalColor);
		}
		else
		{
			email.setForeground(redColor);
			enable = false;
		}
		
		if (isPhoneValid())
		{
			phone.setForeground(normalColor);
		}
		else
		{
			phone.setForeground(redColor);
			enable = false;
		}
		
		createButton.setEnabled(enable);
	}

	private boolean isPhoneValid() {
		return phone.getText() != null && phone.getText().matches("[0-9\\.\\-]+");
	}

	private boolean isEmailValid() {
		return email.getText() != null && email.getText().matches("^[a-zA-Z0-9\\.]+@[a-zA-Z0-9\\.]+");
	}

	private boolean isLevelValid() {
		return level.getText() != null && level.getText().matches("^[345]$");
	}

	private boolean isNameValid() {
		return name.getText() != null && name.getText().length() > 0;
	}

	public void addCreateRowerListener(final Runnable runnable) {
		createButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event arg0) {
				runnable.run();
			}
		});
	}

	public String getEmail() {
		return email.getText();
	}

	public int getLevel() {
		return Integer.valueOf( level.getText() );
	}

	public String getName() {
		return name.getText();
	}

	public String getPhone() {
		return phone.getText();
	}

	public boolean isMember() {
		return isMember.getSelection();
	}

	public void close() {
		newRowerEntryDialog.dispose();
	}
}
